// Widgets/Home/YachtPackage/YachtPackage.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryTravelItem } from '../../../api/travel';
import { queryAppInforMation } from '../../../api/brand';
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    dataList: [],
    page: 0,
    isReachBottom: false,
    noMore: false,
    loading: false,
    loadingFailed: false,
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.getBannerList();
      this.getData();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取轮播图信息
    getBannerList() {
      let _this = this
      queryAppInforMation({infoType: '0'})
        .then(res => {
          if (res.success) {
            _this.setData({
              bannerList: res.data.content
            })
          } else {
            wx.showToast({
              title: res.errMessage,
              icon: 'none'
            })
          }
        })
    },
    // 获取机票套餐列表数据
    getData(){
      let _this = this;
      queryTravelItem({itemType: '4',page: _this.data.page,})
      .then(res => {
        if(res.success){
          var list = _this.data.dataList;
          var arr = [];
          for (let index = 0; index < res.data.content.length; index++) {
            const item = res.data.content[index];
            item.title.imageUrlList = item.title.titleImgUrl.split(',');
            console.log(item.title.imageUrlList,'imageUrlList');
            for (let j = 0; j < item.itemInfo.length; j++) {
              const itm = item.itemInfo[j];
              itm.imgUrlList = itm.imgUrl.split(',');
            }
            arr.push(item);
          }
          if(_this.data.page == 0){
            list = arr;
          }else{
            list = list.concat(arr);
          }
          _this.setData({
            dataList: list,
            total: res.data.totalElements,
            page: _this.data.page+1,
          })
          if(_this.data.page * 10 >= res.data.totalElements){
            _this.setData({
              isReachBottom: false,
              noMore: true,
              loading: false,
              loadingFailed: false,
            })
          }else{
            _this.setData({
              isReachBottom: true,
              noMore: false,
              loading: false,
              loadingFailed: false,
            })
          }
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    // 刷新数据
    onRefresh(){
      this.setData({
        page: 0,
        isReachBottom: false,
        noMore: false,
        loading: false,
        loadingFailed: false,
      })
      this.getData();
    },
    // 加载更多
    onLoadMore(){
      if(this.data.isReachBottom){
        this.getData()
      }
    },
    // 跳转详情
    goDetail(e){
      let _this = this;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      wx.navigateTo({
        url: `${url}?item=${JSON.stringify(item)}`,
        events: {
          'updateData': function(){
            _this.onRefresh();
          }
        }
      })
    }
  }
})